class ClosedQuestion < ActiveRecord::Base
	include AliasingColumns
        
  	set_table_name "CLOSED_QUESTIONS"
	AliasingColumns.add_aliases columns
	
	belongs_to :question,
			   :class_name => "Question",
			   :foreign_key => "question_id"
			   
	has_many :cqpossibleanswers,
			 :class_name => "CQPossibleAnswer",
			 :foreign_key => "closed_question_id",  
 			 :dependent => :destroy
 			 
	accepts_nested_attributes_for :cqpossibleanswers, :allow_destroy => true
	
	validate :answersLimits
	validates_numericality_of :min_answers, :max_answers,:only_integer=>true
	validates_presence_of :min_answers, :max_answers 
	def answersLimits
		if min_answers>max_answers
			errors.add(:max_answers,"Maksymalna liczba odpowiedzi nie moze byc mniejsza od minimalnej!")
		end
	end
	
	def stats
		result =Hash.new
		sum=(self.question.survey.number_of_ready).to_f
		for possAnswer in self.cqpossibleanswers
			result[possAnswer.possibleAnswer]=((possAnswer.cqanswers.length/sum)*100).to_s<<"%"		
		
		end
		return result
	end
	
	
	if  AliasingColumns.get_adapter_name == "oracle"
	#	self.primary_key ="ID"
		set_sequence_name table_name()<<"_SEQ" 
	#	before_create :set_id
		def set_id
			 self.ID =connection.next_sequence_value(self.class.sequence_name)	
		end 
	end


end
